package net.sinmetal.twitcockpit.model.config;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.Model;

/**
 * Cockpit搭乗許可ユーザ
 *
 * @author Sinmetal
 *
 */
@Model(schemaVersion = 1)
public class AuthUser implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;

    @Attribute(unindexed = true)
    private List<String> emails;

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    /**
     * @return emails
     */
    public List<String> getEmails() {
        return emails;
    }

    /**
     * @param emails
     *            セットする emails
     */
    public void setEmails(List<String> emails) {
        this.emails = emails;
    }

    /**
     * AuthUserのEmailを追加 重複があった場合、無視する
     *
     * @param emails
     */
    public void addAuthUser(List<String> emails) {
        for (String email : emails) {
            addAuthUser(email);
        }
    }

    /**
     * AuthUserのEmailを追加 重複があった場合、無視する
     *
     * @param email
     */
    public void addAuthUser(String email) {
        if (this.emails.contains(email)) {
            return;
        }
        this.emails.add(email);
    }

    /**
     * Userが承認済みかを返す
     *
     * @param email
     * @return
     */
    public boolean constains(User user) {
        if (user == null) {
            return false;
        }
        return constains(user.getEmail());
    }

    /**
     * Emailが承認済みかを返す
     *
     * @param email
     * @return
     */
    public boolean constains(String email) {
        return this.emails.contains(email);
    }

    /**
     * Key生成
     *
     * @return
     */
    public static Key createKey() {
        return Datastore.createKey(AuthUser.class, 1);
    }

    /**
     * インスタンス生成
     *
     * @return
     */
    public static AuthUser getInstance() {
        AuthUser model = new AuthUser();
        model.emails = new ArrayList<String>();
        return model;
    }

    /**
     * インスタンス生成
     *
     * @param emails
     * @return
     */
    public static AuthUser getInstance(List<String> emails) {
        AuthUser model = new AuthUser();
        model.setEmails(emails);
        return model;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        AuthUser other = (AuthUser) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}
